package cn.org.xwb.bookmall.user.controller;

import cn.org.xwb.bookmall.model.constant.AccountConst;
import cn.org.xwb.bookmall.model.service.impl.CommentsServiceImpl;
import cn.org.xwb.bookmall.model.service.impl.ProductServiceImpl;
import cn.org.xwb.bookmall.model.vo.CommentsVo;
import cn.org.xwb.bookmall.model.vo.ProductConditionVo;
import cn.org.xwb.bookmall.model.vo.ProductVo;
import cn.org.xwb.bookmall.user.annotation.IsLoginParam;
import cn.org.xwb.bookmall.user.service.UserProductService;
import cn.org.xwb.common.encrypt.annotation.des.DesEn;
import cn.org.xwb.common.encrypt.annotation.rsa.RsaDe;
import cn.org.xwb.common.encrypt.annotation.rsa.RsaDeObj;
import cn.org.xwb.common.encrypt.annotation.rsa.RsaDeParam;
import cn.org.xwb.common.encrypt.constant.EncryptConstant;
import cn.org.xwb.common.error.annotation.ServerErrorCatch;
import cn.org.xwb.common.model.vo.RespResult;
import cn.org.xwb.common.page.dto.PageResult;
import cn.org.xwb.common.valid.annotation.nu.NotNull;
import cn.org.xwb.common.valid.annotation.nu.NotNullObj;
import cn.org.xwb.common.valid.annotation.nu.NotNullParam;
import cn.org.xwb.common.valid.annotation.str.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/product")
public class UserProductController {

    @Autowired
    private UserProductService userProductService;

    @Autowired
    private ProductServiceImpl productService;

    /**
     * 获取全部的商品分类
     *
     * @param desKey
     * @return
     */
    @GetMapping("/category/all")
    @ServerErrorCatch
    @StrNotNull
    @RsaDe
    @DesEn
    public RespResult<Object> getAllProductCategory(
            @RequestParam(EncryptConstant.DES_KEY_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String desKey
    ) {
        return userProductService.getProductCategoryAll();
    }

    /**
     * 随机获取指定个数商品的商品列表
     *
     * @param productCount
     * @param productCategoryId
     * @param productCategorySubId
     * @return
     */
    @GetMapping("/random")
    @NotNull
    @StrNotNull
    @RsaDe
    @DesEn
    public RespResult<Object> getProductListRandom(
            @RequestParam(EncryptConstant.DES_KEY_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String desKey,
            @RequestParam("productCount") @NotNullParam
            Long productCount,
            @RequestParam(value = "productCategoryId" ,required = false)
            Long productCategoryId,
            @RequestParam(value = "productCategorySubId",required = false)
            Long productCategorySubId
    ) {
        return userProductService.getProductListRandom(productCount, productCategoryId, productCategorySubId);
    }

    /**
     * 根据 id 获取商品信息
     *
     * @param desKey
     * @param productId
     * @return
     */
    @GetMapping("/{productId}")
    @NotNull
    @StrNotNull
    @RsaDe
    @DesEn
    public RespResult<Object> getProductById(
            @RequestParam(EncryptConstant.DES_KEY_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String desKey,
            @PathVariable("productId") @NotNullParam
            Long productId
    ) {
        return userProductService.getProductById(productId);
    }

    @GetMapping("/kw")
    @ServerErrorCatch
    @StrNotNull
    @RsaDe
    @DesEn
    public RespResult<Object> getProductByKw(
            @RequestParam(EncryptConstant.DES_KEY_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String desKey,
            @StrNotNullParam @RsaDeParam
            String kw
    ) {
        List<ProductVo> productVoList = productService.findAllFromCache();
        // 筛选出未被逻辑删除的书籍商品
        if (Objects.nonNull(productVoList) && productVoList.size() > 0) {
            productVoList = productVoList.stream().filter(
                    productVo -> productVo.getIsRemove() == false
            ).toList();
        }
        // 未被逻辑删除的书籍商品不为空
        if (Objects.nonNull(productVoList) && productVoList.size() > 0) {
            // productVoList = productVoList.stream().filter(productVo -> productVo.getTitle().contains(kw)).toList();
            ArrayList<ProductVo> productVos = new ArrayList<>();
            for (ProductVo productVo : productVoList) {
                // 根据搜索关键词搜索书籍商品
                if (
                        productVo.getTitle().contains(kw) ||
                                productVo.getCode().contains(kw) ||
                                (productVo.getProductCategory()
                                        + productVo.getProductCategorySub()
                                ).contains(kw)
                ) {
                    productVos.add(productVo);
                }
            }
            productVoList = productVos;
        }
        return RespResult.success("获取商品列表成功", productVoList);
    }

    @Autowired
    private CommentsServiceImpl commentsService;

    @GetMapping("/comment")
    @ServerErrorCatch
    @StrNotNull
    @NotNull
    @RsaDe
    @DesEn
    public RespResult<Object> getComment(
            @RequestParam(EncryptConstant.DES_KEY_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String desKey,
            @RequestParam(value = "productId", required = false)
            Long productId,
            @RequestParam(value = "userId", required = false)
            Long userId
    ) {
        List<CommentsVo> comments = commentsService.getComments(productId, userId, null);
        return RespResult.success("获取商品评论列表成功", comments);
    }

    @PostMapping("/comment/del")
    @ServerErrorCatch
    @StrNotNull
    @NotNull
    @RsaDe
    @IsLoginParam
    public RespResult<Object> delComment(
            @RequestParam(AccountConst.ACCOUNT_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String account,
            @RequestParam(value = "commentId", required = false)
            Long commentId
    ) {
        Boolean res = commentsService.delComment(commentId);
        if (!res) return RespResult.success("获取商品评论删除失败");
        return RespResult.success("获取商品评论删除成功");
    }
}
